package com.crowntit.domain.activity.service.trail.node;

import com.crowntit.domain.activity.model.entity.MarketProductEntity;
import com.crowntit.domain.activity.model.entity.TrialBalanceEntity;
import com.crowntit.domain.activity.service.trail.AbstractGroupBuyMarketSupport;
import com.crowntit.domain.activity.service.trail.factory.DefaultActivityStrategyFactory;
import com.crowntit.types.design.framework.tree.StrategyHandler;
import com.crowntit.types.enums.ResponseCode;
import com.crowntit.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RootNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private SwitchNode switchNode;

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        String userId = requestParameter.getUserId();
        String goodsId = requestParameter.getGoodsId();
        String source = requestParameter.getSource();
        String channel = requestParameter.getChannel();

        if (StringUtils.isAnyBlank(userId, goodsId, source, channel)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return switchNode;
    }
}
